

//客户端代码编写
#include <iostream>
#include <memory>
#include <string>
#include "Socket.hpp"
#include "Protocol.hpp"
#include "Common.hpp"

using namespace SocketModule;
void GetDataFromStdin(int* x,int* y,char* oper)
{
    std::cout << "Please Enter x: ";
    std::cin >> *x;
    std::cout << "Please Enter y: ";
    std::cin >> *y;
    std::cout << "Please Enter oper: ";
    std::cin >> *oper;
}
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        std::cerr<<" usage "<<argv[0]<<" server_ip ,server_port"<<std::endl;
        return 1;
    }
    std::string server_ip=argv[1];
    uint16_t port=std::stoi(argv[2]);

    std::shared_ptr<Socket> client=std::make_shared<TcpSocket>();
    client->BuildTcpClientMethod();

    if(client->Connect(server_ip,port)!=0)
    {
        //连接失败
        std::cerr<<"连接失败"<<std::endl;
        exit(CONNECT_ERR);
    }
    std::unique_ptr<Protocol> protocol=std::make_unique<Protocol>();
    std::string resp_buffer;

    while(true)
    {
        int x,y;
        char oper;
        GetDataFromStdin(&x,&y,&oper);

        //构建一个请求
        std::string rep_str=protocol->BuildRequest(x,y,oper);

        //发送请求
        client->Send(rep_str);

        //获取应答,同时完成填充resp
        //resp中就包含运算结果
        Response resp;
        bool ok=protocol->GetResponse(client,resp_buffer,&resp);
        if(ok==false)
        break;

        //显示结果
        resp.show();
    }
    return 0;
}